/**
 * @file        main.c
 *
 * @brief       GPIO Toggle Example
 *
 * @date        Nov 26, 2012 11:37:19 PM
 *
 * @author      Ashwin Vijayakumar
 *
 * @copyright   (C) 2012 Ashwin Vijayakumar <ashwin@afflatustech.com>
 * @copyright   (C) 2012 Afflatus Techtronics <www.afflatustech.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * @details     This example code shows how to configure a GPIO pin as output
 *              and toggle it.
 *
 * Test bed:            STM32F4Discovery with STM32F407VGT6
 * Compatibility:       Any STM32F4xx chip
 * GPIO:                GPIOD PIN15 (LD6 on STM32F4Discovery)
 *
 * Usage:       Rename this file as main.c, move it to ./app/src, compile
 *              project and flash binary.
 */

/* ---- System headers ------------------------------------------------------ */
/* ---- Local headers ------------------------------------------------------- */
#include "stm32f4xx.h"
#include "gpio.h"

/* ---- Typedef section (lower_case_type) ----------------------------------- */
/* ---- Constants section (ALL_CAPS) ---------------------------------------- */
/* ---- Macros section (ALL_CAPS) ------------------------------------------- */
/* ---- Global variables section (lower_case) ------------------------------- */
/* ---- Function prototypes section (lower_case) ---------------------------- */
/* ---- Getter/setter method section (get/set/is_lower_case) ---------------- */

/**
 * @fn          int main( void )
 */

int
main( void )
{
  uint32_t dum_delay;

  /* Enable IO port D (GPIOD) clock */
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN;

  /* Set GPIOD Pin15 to General purpose output mode */
  GPIOD->MODER |= GPIO_MODER_MODER15_0;
  GPIOD->MODER &= ~GPIO_MODER_MODER15_1;

  /* Set GPIOD Pin15 to Output push-pull */
  GPIOD->OTYPER &= ~( GPIO_OTYPER_OT_15 );

  /* Set GPIOD Pin15 to 100 MHz High speed */
  GPIOD->OSPEEDR |= ( GPIO_OSPEEDER_OSPEEDR15_1 | GPIO_OSPEEDER_OSPEEDR15_0 );

  /* Set GPIOD Pin15 to No pull-up, pull-down */
  GPIOD->PUPDR &= ~( GPIO_PUPDR_PUPDR15_1 | GPIO_PUPDR_PUPDR15_0 );

  /* Loop forever */
  while( 1 )
    {
      /* Set GPIOD Pin15 to high */
      GPIOD->ODR ^= GPIO_ODR_ODR_15;
      for( dum_delay = 0; dum_delay < (2*1000000); dum_delay++ );
    }

  return( 0 );
}

/* ==== End of file ================= Copyright (C) 2012 Afflatus Techtronics */
